package com.neuedu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileUploadController {


    @PostMapping("upload")
    String upload(MultipartFile pic) throws IOException {
        //1 将临时空间的文件，转储到 D:\\upload
        String originalFileName = pic.getOriginalFilename();
        //扩展名
        String ext = originalFileName.substring(originalFileName.lastIndexOf("."));
        //转储的新文件名
        String newFileName = UUID.randomUUID().toString() + ext;
        //转储到 D:\\upload 下的  uuid.png/uuid.jpg
        pic.transferTo(new File(BootConstants.UPLOAD_DIR, newFileName));
        //2 将数据存储到 db
        /**
         * 当前时间
         * 上传的ip
         * 操作的工号
         * 上传的文件名
         *
         */

        //3 跳转到列表页

        return "redirect:/list";
    }


    @GetMapping("list")
    String list(Model model) throws IOException {

        File[] list = new File(BootConstants.UPLOAD_DIR).listFiles();

        model.addAttribute("fileList",list);

        return "/list.jsp";
    }


}
